//
//  PostViewController.m
//  礼物go
//
//  Created by dllo on 16/5/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PostViewController.h"
#import "TopicTableViewCell.h"
#import "CycleColumnViewController.h"
#import "TopicOtherTableViewCell.h"
#import "TopicOtherTModel.h"
#import "PostColumnViewController.h"
#import "AllColumnViewController.h"

//static NSString *const postCell = @"postCell";
static NSString *const topicTCell = @"topicTCell";
static NSString *const topicOtherCell = @"topicTOtherCell";

@interface PostViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *postTableView;
@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,strong)UIView *headerView;
@property(nonatomic,strong)UIButton *headerButton;

@end

@implementation PostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.dataArray = [NSMutableArray array];
    
    
self.postTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, self.view.frame.size.height - 113) style:UITableViewStylePlain];
self.postTableView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFAFA,0xBEBEBE);
    [self.view addSubview:self.postTableView];
    self.postTableView.dataSource = self;
    self.postTableView.delegate = self;
    //[self.postTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:postCell];
    [self.postTableView registerClass:[TopicTableViewCell class] forCellReuseIdentifier:topicTCell];
[self.postTableView registerClass:[TopicOtherTableViewCell class] forCellReuseIdentifier:topicOtherCell];
    self.postTableView.showsVerticalScrollIndicator = NO;
    
    [self getHearderView];
    [self getThreeSectionsData];
    
    
    // Do any additional setup after loading the view.
}

-(void)getThreeSectionsData{
NSString *urlStr = @"http://api.liwushuo.com/v2/channel_groups/all";
    [LYNetworking ly_GetWithURL:urlStr Parameter:nil success:^(id result) {
        NSDictionary *dataDic = [result objectForKey:@"data"];
        NSArray *channelsArray = [dataDic objectForKey:@"channel_groups"];
        for (NSDictionary *dic in channelsArray) {
            TopicOtherTModel *SectionModel = [[TopicOtherTModel alloc] init];
            [SectionModel setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:SectionModel];
            
        }
        [self.postTableView reloadData];
        
    } error:^(NSError *error) {
        
        
    }];
}

-(void)getHearderView{
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 30)];
    self.headerView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFAFA,0xBEBEBE);
    self.postTableView.tableHeaderView = self.headerView;
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, self.headerView.frame.size.width / 2, self.headerView.frame.size.height)];
    nameLabel.text = @"专题";
   // nameLabel.font = [UIFont systemFontOfSize:18];
    [self.headerView addSubview:nameLabel];
    
    
    self.headerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.headerButton.frame = CGRectMake(self.headerView.frame.size.width - 100, 15, 100, self.headerView.frame.size.height - 20);
   // self.headerButton.backgroundColor = [UIColor yellowColor];
    [self.headerButton setTitle:@"查看全部>" forState:UIControlStateNormal];
    [self.headerButton setTitleColor:[UIColor grayColor] forState: UIControlStateNormal];
    [self.headerView addSubview:self.headerButton];
    [self.headerButton addTarget:self action:@selector(headerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.headerButton.titleLabel.font = [UIFont systemFontOfSize:15];
}

-(void)headerButtonAction:(UIButton *)button{
    AllColumnViewController *allVC = [[AllColumnViewController alloc] init];
    [self.navigationController pushViewController:allVC animated:YES];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count + 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return 15;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      __block PostViewController *postVC = self;
    if (indexPath.section == 0) {
        TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:topicTCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
      //cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFAFA,0xBEBEBE);
        cell.topicBlock = ^(NSString *dataId){
    CycleColumnViewController *cycVC = [[CycleColumnViewController alloc] init];
            cycVC.dataId = dataId;
            [postVC.navigationController pushViewController:cycVC animated:YES];
        };
        return cell;
    }
    
    TopicOtherTModel *topicSection = [self.dataArray objectAtIndex:indexPath.section - 1];
        TopicOtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:topicOtherCell];
    cell.nameLabel.text = topicSection.name;
    //cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFAFA,0xBEBEBE);
    cell.dataArray = topicSection.channels;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.PostColumnBlock = ^(NSNumber *myId,NSString *name){
        PostColumnViewController *postCVC = [[PostColumnViewController alloc] init];
        postCVC.dataId = myId;
        postCVC.titleName = name;
        [postVC.navigationController pushViewController:postCVC animated:YES];
    
    };
        return cell;
  
    
   }

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 90;
    }
    if (indexPath.section == 1) {
       return 426;
    }
    if (indexPath.section == 2) {
        return 226;
    }
    
    return 326;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
